package com.wuseguang.springcloud.timer;


import com.alibaba.fastjson.JSONObject;
import com.wuseguang.springcloud.JdbcTemplate.WXJdbc;
import com.wuseguang.springcloud.dao.jdbc.TeacherSubscriptionJdbc;
import com.wuseguang.springcloud.util.DateUtil;
import com.wuseguang.springcloud.util.WeChat.TemplateTools;
import com.wuseguang.springcloud.util.WeChat.WXUtil;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 教师订阅课表
 * @author
 * @since 2020-05-26
 */
public class timetableTimer extends QuartzJobBean {

    @Autowired
    private TeacherSubscriptionJdbc teacherSubscriptionJdbc;
    @Autowired
    private WXJdbc wXJdbc;
    /**
     * 执行时推送信息给教师（上课时间到了）
     * @param jobExecutionContext
     * @throws JobExecutionException
     */
    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        String schoolAndY = jobExecutionContext.getTrigger().getKey().getGroup();
        System.err.println("开始推送信息 学校id-节次id"+schoolAndY);

        //拆分数据 0学校id,1节次
        String []  sy = schoolAndY.split("-");
        //星期
        Integer x =DateUtil.getWeeksInt(DateUtil.gettime());
        //查询今天这节课有订阅推送的教师信息
        String sql = "select t.course_name,t.teacher_name,c.`name` className,t.teacher_id from " +
                " teacher_subscription ts left join timetable t on(t.teacher_id=ts.teacher_id)" +
                " LEFT JOIN classes c on(c.id=t.class_id) where t.school_id=? and t.y=? and x=?" +
                " and ts.id is not null and ts.state = 1 and ts.type='timetable'";
        //教师上课信息
        List<Map<String,Object>> list = teacherSubscriptionJdbc.queryListMap(sql,sy[0],sy[1],x);
        //获取token
        String accessToken = wXJdbc.getOfficialAccountAccessToken(0);

        sql = "select start_time,end_time from attend_class_time where school_id =? and current_section = ?";
        //上课时间
        Map<String,Object> timeMap =  teacherSubscriptionJdbc.queryMap(sql,sy[0],sy[1]);
        //循环上课数据
        for (Map<String,Object> map:list) {

            sql = "select open_id_one,open_id_two from teacher where id = ?";
            Map<String,Object> openIdMap =  teacherSubscriptionJdbc.queryMap(sql,map.get("teacher_id"));
            //教师公众号openId
            String openId = openIdMap.get("open_id_two").toString();
            //判断openId是否为空
            if(!StringUtils.isEmpty(openId)){
                Map<String, Object> pushMap = new HashMap<>();
                pushMap.put("first", "尊敬的教师您好,您将在五分钟后上课");
                pushMap.put("keyword1", map.get("course_name").toString());
                pushMap.put("keyword2", "第"+(Integer.valueOf(sy[1])+1)+"节课["+timeMap.get("start_time")+"-"+timeMap.get("end_time")+"]");
                pushMap.put("keyword3", map.get("className").toString());
                pushMap.put("remark", "感谢您的支持。");

                //跳转小程序信息
                JSONObject appletJson= new JSONObject();
          /*  appletJson.put("appid","wx30b606bac7775eba");
            appletJson.put("pagepath","pages/evaluate/index");*/

                //解析后的数据
                JSONObject json = TemplateTools.initializationData(pushMap);
                //推送公众号信息
                WXUtil.officialAccountPush(accessToken, openId, "q_VcUTdajtd9uJhmj0Qd3ldDSrzRmHJQhXmXNHzPrN4", "", appletJson, json);
            }
        }

    }
}
